package swc.smu.ui;

import android.support.design.widget.AppBarLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import swc.smu.adapter.RecyclerAdapter;
import swc.smu.ui.custom.SelectImageView;

public class RecyclerActivity extends BaseActivity {

    RecyclerView recyclerView;
    RecyclerAdapter adapter;

    Toolbar toolbar;
    AppBarLayout appBarLayout;

    SelectImageView selectImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler);
        initView();
        initToolbar();
    }

    private void initToolbar() {
        appBarLayout = (AppBarLayout) findViewById(R.id.recycler_appbar_layout);
        toolbar = (Toolbar) findViewById(R.id.recycler_toolbar);
        setSupportActionBar(toolbar);
        final ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

                int height = getActionBarHeight();
                if (height + verticalOffset <= (height / 2)) {
                    actionBar.setDisplayHomeAsUpEnabled(false);
                    actionBar.setDisplayShowTitleEnabled(false);
                } else {
                    actionBar.setDisplayHomeAsUpEnabled(true);
                    actionBar.setDisplayShowTitleEnabled(true);
                }
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.onBackPressed();
                break;
        }
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.recycleractivity_recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        List<String> lists = new ArrayList<String>();
        for (int i = 0; i < 100; i++) {
            lists.add("item " + i);
        }
        adapter = new RecyclerAdapter(this, lists);
        recyclerView.setAdapter(adapter);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new ItemDivider());

        selectImageView = (SelectImageView) findViewById(R.id.select_dialog_listview);
    }
}
